//
//  ACMacro.h
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//  Updated by caoyq on 2018/11/22.
//

#ifndef ACMacro_h
#define ACMacro_h

/// 弱引用 ##连接词
#define ACWeak(type)    __weak __typeof(type) weak##type = type;
#define ACWeakSelf      __weak __typeof(self) weakSelf = self;
/// 强引用 紧跟着weakSelf使用
#define ACStrong(type)  __strong __typeof(weak##type) strong##type = weak##type;
#define ACStrongSelf    __strong __typeof(weakSelf) self = weakSelf;

/// 日志输出
#ifdef DEBUG
#define ACLog(format, ...)  fprintf(stderr, "\n---[时间:%s]\n---[方法名:%s]\n---[行号:%d]\n---打印内容:\n%s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ##__VA_ARGS__] UTF8String]);

#else
#define ACLog(...)
#endif

///block相关宏 <block ? block() : nil> 的快捷方式
#define ACSafeBlock(block, ...) block ? block(__VA_ARGS__) : nil;

#pragma mark - 沙盒文件路径

#define AC_DocumentPath     NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject
#define AC_CachePath        NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject

#pragma mark - 判断 iPhone系统

#define AC_SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define AC_SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define AC_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define AC_SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define AC_SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define AC_iOS7Later  ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
#define AC_iOS8Later  ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
#define AC_iOS9Later  ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define AC_iOS10Later ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f)
#define AC_iOS11Later ([UIDevice currentDevice].systemVersion.floatValue >= 11.0f)

#pragma mark - 尺寸适配

/// 屏幕尺寸
#define AC_ScreenBounds [UIScreen mainScreen].bounds
#define AC_ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define AC_ScreenHeight [UIScreen mainScreen].bounds.size.height

///状态栏（iphoneX 44, normal 20）
#define AC_StatusBarHeight   [UIApplication sharedApplication].statusBarFrame.size.height
//导航栏
#define AC_NavBarHeight      44.0f
//导航栏 + 状态栏
#define AC_NavTopHeight      (AC_StatusBarHeight + AC_NavBarHeight)
//iphoneX底部安全距离
#define AC_SafeAreaBottomMargin  34.0f
//底部安全区域设置
#define AC_BottomMargin      (AC_IS_IPHONE_X ? AC_SafeAreaBottomMargin : 0.0f)
//tabbar
#define AC_TabBarHeight      (49.0f + AC_BottomMargin)

///按照4.7寸(375 x 667)为基准 来做适配
#define AC_ScaleSize    AC_ScreenWidth / 375.0
#define ACScale(number) AC_ScaleSize * (number)

//屏幕适配（320、480）（320、568）（375、667）（414、736）（375、812）
#define AC_IS_IPHONE_4S ([UIScreen mainScreen].bounds.size.height == 480 ? YES : NO)
#define AC_IS_IPHONE_5S ([UIScreen mainScreen].bounds.size.height == 568 ? YES : NO)
#define AC_IS_IPHONE_6  ([UIScreen mainScreen].bounds.size.height == 667 ? YES : NO)
#define AC_IS_IPHONE_6P ([UIScreen mainScreen].bounds.size.height == 736 ? YES : NO)

//识别 iphoneX/XR/XS/XS Max(如下写法能避免Xcode10出现的警告)
#define AC_IS_IPHONE_X \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})

#pragma mark - 简写

/* 颜色 */

/// RGB颜色  e.g. ACRGBColor(100, 100, 100)
#define ACRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
///a 透明度
#define ACRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
///hex颜色(十六进制) e.g. ACHEXColor(0x509836)
#define ACHEXColor(hex) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:1.0]
///a 透明度
#define ACHEXAColor(hex, a) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:a]

/* 字体 */

#define ACFont(fontValue)      [UIFont systemFontOfSize:fontValue]
#define ACBoldFont(fontValue)  [UIFont boldSystemFontOfSize:fontValue]

//获取图片
#define ACImage(imageName)       [UIImage imageNamed:imageName]
//使用原图 UIBarButtonItem 设置图片 用得上
#define ACOriginalImage(imageName) [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
//TODO: - 错了到时候再改
#define ACImagePath(path, type) [[NSBundle mainBundle] pathForResource:path ofType:type];

//UINib创建简写，传入类即可
#define ACNibFromClass(className)     [UINib nibWithNibName:ACStringFromClass(className) bundle:nil]
//根据类得到字符串类名简写，传入类即可
#define ACStringFromClass(className)  NSStringFromClass([className class])


#pragma mark - 单例宏定义

/*
 .h 中使用 ACSingleton_h(name)
 .m 中使用 ACSingleton_m(name)
 */
#define ACSingleton_h(name) + (instancetype)name;

#define ACSingleton_m(name) \
static id _instance; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instance; \
}

#endif
